package com.service;

import com.entity.Order;
import com.entity.Soldrecord;
import com.mapper.OrderMapper;
import com.mapper.SoldrecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 订单 服务类
 */
@Service
@Transactional
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SoldrecordMapper soldrecordMapper;

    /**添加订单*/
    public Integer insertOrder(Order order){
        return orderMapper.insertOrder(order);
    }
    /**分页查看所有订单内容*/
    public List<Order> queryAllOrder(Integer page, Integer count, String userid){
        return orderMapper.queryAllOrder(page,count,userid);
    }
    /**分页查看订单详情*/
    public List<Order> queryAllOrderItems(String orderid){
        return orderMapper.queryAllOrderItems(orderid);
    }
    /**生成订单*/
    public List<Order> formOrder(String userid){
        return orderMapper.formOrder(userid);
    }
    /**生成存储订单*/
    public int formRealOrder(Order order){
        orderMapper.insertOrder(order);
        List<Order> orders = orderMapper.formOrder(order.getUserid());
        Soldrecord soldrecord = new Soldrecord();
        soldrecord.setId(order.getOrderid()).setSoldtime(order.getFormtime()).setSoldstatus(1);
        int flag=1;
        for(int i=0;i<orders.size();i++){
            orders.get(i).setOrderid(order.getOrderid());
            flag = orderMapper.insertOrderItem(orders.get(i));
            if(flag==0){
                break;
            }
            BigDecimal thinkmoney = new BigDecimal(Float.toString(orders.get(i).getThinkmoney()));
            soldrecord.setCommid(orders.get(i).getCommid()).setCommname(orders.get(i).getCommname()).setCommdesc(orders.get(i).getCommdesc()).setThinkmoney(thinkmoney).setNumber(orders.get(i).getNumber()).setUserid(orders.get(i).getSolderid());
            soldrecordMapper.insertSold(soldrecord);
        }
        orderMapper.deleteCart(order.getUserid());
        return flag;
    }
    /**修改订单物品状态*/
    public Integer updateOrder(Order order){
        return orderMapper.updateOrder(order);
    }
    /**查询生成订单商品数*/
    public Integer queryFormOrderCount(String userid){
        return orderMapper.queryFormOrderCount(userid);
    }
    /**查询我的订单总数*/
    public Integer queryOrderCount(String userid){
        return orderMapper.queryOrderCount(userid);
    }
    /**查看单个订单*/
    public Order queryMainOrder(String orderid){
        return orderMapper.queryMainOrder(orderid);
    }
    /**查询订单商品总数*/
    public Integer queryOrderItemsCount(String orderid){
        return orderMapper.queryOrderItemsCount(orderid);
    }
    /**删除订单*/
    public Integer deleteFromOrder(String orderid){
        return orderMapper.deleteOrder(orderid);
    }
}